﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class ContactService
    {
        public readonly SqlSugarClient _db;

        public ContactService(SqlSugarHelper sqlSugarHelper)
        {
            _db = sqlSugarHelper.sugarClient;
        }

        public List<ContactEntity> GetAllContacts(int pageNumber,int pageSize,ref int totalNumber)
        {
            return _db.Queryable<ContactEntity>()
                 .OrderBy(c=> c.Sort)
                .ToPageList(pageNumber, pageSize,ref totalNumber);
        }

        public async Task<List<ContactEntity>> GetAllContactsAsync(bool? display = null,bool? rightDisplay = null)
        {
            return await _db.Queryable<ContactEntity>()
                 .WhereIF(rightDisplay != null, c => c.RightDisplay == rightDisplay)
                 .WhereIF(display != null, c => c.Display == display)
                 .OrderBy(c=> c.Sort)
                 .ToListAsync();
        }

        public ContactEntity GetContactById(int id)
        {
            return _db.Queryable<ContactEntity>().InSingle(id);
        }

        public ContactEntity AddContact(ContactEntity contact)
        {
            contact.CreateDate = DateTime.Now;
            contact.Sort = GetContactSingleMaxSort();
            return _db.Insertable(contact).ExecuteReturnEntity();
        }

        public void UpdateContact(ContactEntity contact)
        {
            contact.CreateDate = DateTime.Now;
            _db.Updateable(contact).ExecuteCommand();
        }

        public void DeleteContact(int id)
        {
            _db.Deleteable<ContactEntity>().Where(c => c.Id == id).ExecuteCommand();
        }

        public void ContactDisplay(int id,bool display,bool rightDisplay)
        {
            var contact =  GetContactById(id);
            if (contact == null)
            {
                throw new Exception($"ID {id} 找不到对象");
            }
            contact.Display = display;
            contact.RightDisplay = rightDisplay;
            _db.Updateable(contact)
                .ExecuteCommand();
        }

        public ContactEntity GetContactSingleBySortDesc(int sort)
        {
            return _db.Queryable<ContactEntity>()
                .Where(c => c.Sort < sort)
                .OrderBy(c => c.Sort, OrderByType.Desc)
                .First();
        }

        public ContactEntity GetContactSingleBySort(int sort)
        {
            return _db.Queryable<ContactEntity>()
                .Where(c => c.Sort > sort)
                .OrderBy(c => c.Sort)
                .First();
        }

        public int GetContactSingleMaxSort()
        {
            var contact = _db.Queryable<ContactEntity>()
                .OrderBy(c => c.Sort, OrderByType.Desc)
                .First();
            if (contact != null)
            {
                return contact.Sort + 1;
            }
            return 0;
        }

        public void ChangeSort(int id,int sort)
        {
            var contact = GetContactById(id);
            if (contact == null)
            {
                throw new Exception($"ID {id} 找不到对象");
            }
            var tempSort = contact.Sort;

            if (sort < 0)
            {
                var prevContact = GetContactSingleBySortDesc(tempSort);
                if (prevContact != null)
                {
                    contact.Sort = prevContact.Sort;
                    prevContact.Sort = tempSort;
                    UpdateContact(contact);
                    UpdateContact(prevContact);
                }
            }
            else
            {
                var nextContact = GetContactSingleBySort(tempSort);
                if (nextContact != null)
                {
                    contact.Sort = nextContact.Sort;
                    nextContact.Sort = tempSort;
                    UpdateContact(contact);
                    UpdateContact(nextContact);
                }
            }
        }
    }
}
